﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Web.ASPxGridView;

namespace TravelLink.WebApp.UserControl.Member.JobEditable {
    public abstract class JobEditableBase {

        protected bool _isEn;

        public JobEditableBase(bool isEn) { this._isEn = isEn; }

        public virtual void Initialize(Func<string, bool> checkIsEditing, Action<string, object> bindDelegate, ResumeParam p) {
            if (!checkIsEditing(AspGridViewName)) {
                DataSourceBind(bindDelegate, p);
            }
        }


        protected void DataSourceBind(Action<string, object> bindDelegate, ResumeParam p) {
            bindDelegate(AspGridViewName, GetSource(p));
        }

        public void RowDeleting(Action<string, object> bindDelegate, ResumeParam p, DevExpress.Web.Data.ASPxDataDeletingEventArgs e, ASPxGridView sender) {
            PreDeleting(p, e, sender);
            DataSourceBind(bindDelegate, p);
        }

        public void RowUpdating(Action<string, object> bindDelegate, ResumeParam p, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e, ASPxGridView sender) {
            PreUpdating(p, e, sender);
            DataSourceBind(bindDelegate, p);
        }

        public void RowInserting(Action<string, object> bindDelegate, ResumeParam p, DevExpress.Web.Data.ASPxDataInsertingEventArgs e, ASPxGridView sender) {
            PreInserting(p, e, sender);
            DataSourceBind(bindDelegate, p);

        }

        public void RowSave(ResumeParam p, Action<string, object> bindDelegate) {
            this.Save(p);
            DataSourceBind(bindDelegate, p);
        }

        public abstract string AspGridViewName { get; }

        protected abstract IEnumerable<object> GetSource(ResumeParam p);

        public abstract void Save(ResumeParam p);

        protected abstract void PreDeleting(ResumeParam p, DevExpress.Web.Data.ASPxDataDeletingEventArgs e, ASPxGridView sender);

        protected abstract void PreUpdating(ResumeParam p, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e, ASPxGridView sender);

        protected abstract void PreInserting(ResumeParam p, DevExpress.Web.Data.ASPxDataInsertingEventArgs e, ASPxGridView sender);


        protected IEnumerable<EditableItem<T>> List<T>(List<T> dataList) {
            List<EditableItem<T>> temp = new List<EditableItem<T>>(dataList.Count);

            foreach (var item in dataList) {
                temp.Add(Add<T>(item, EditableItemStatus.Default));
            }

            return temp;
        }


        protected EditableItem<T> Add<T>(T entity, EditableItemStatus status) {
            return new EditableItem<T> {
                Item = entity,
                Status = status
            };
        }

        protected List<EditableItem<T>> Get<T>(Func<EditableItem<T>, bool> filter) {

            List<EditableItem<T>> memoryList = Get(AspGridViewName) as List<EditableItem<T>>;
            if (memoryList != null && memoryList.Count > 0) {
                return memoryList.Where(filter).ToList();
            }
            else {
                return new List<EditableItem<T>>();
            }
        }

        protected void Set(string name, object val) {
            HttpContext.Current.Session[name] = val;
        }

        public void Clear(string name) {
            HttpContext.Current.Session.Remove(name);
        }

        protected object Get(string name) {
            return HttpContext.Current.Session[name];
        }

        protected bool Has(string name) {
            return HttpContext.Current.Session[name] != null;
        }

    }
}